package com.segi.view.rubbler;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.segi.view.R;

/**
 * 刮刮乐自定义view
 * Created by Kinwee on 2016/11/15.
 */
public class RubblerView extends View {

    private boolean isMove = false;
    private Bitmap bitmap = null;
    private Bitmap frontBitmap = null;
    private Path path;
    private Canvas mCanvas;
    private Paint paint;

    private int paintSize = 25;

    private int frontBitmapResource;

    private Context mContext;


    public RubblerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs,
                    R.styleable.RubblerView, 0, 0);
            frontBitmapResource = a.getResourceId(R.styleable.RubblerView_bitmap, frontBitmapResource);
            paintSize = a.getInt(
                    R.styleable.RubblerView_paint_size,
                    paintSize);
            a.recycle();
        }
        mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (mCanvas == null) {
            EraseBitmp();
        }
        canvas.drawBitmap(bitmap, 0, 0, null);
        mCanvas.drawPath(path, paint);//绘制一个路径
        super.onDraw(canvas);
    }

    public void EraseBitmp() {

        bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);

        if (frontBitmapResource == 0) {
            frontBitmap = CreateBitmap(Color.GRAY, getWidth(), getHeight());//生产灰色图片
        }
        //设置画笔的样式
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔的风格：空心
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//设置图片交叉时的显示模式
        paint.setAntiAlias(true);//防据齿
        paint.setDither(true);//防抖动
        paint.setStrokeJoin(Paint.Join.ROUND);//设置结合处的样子，ROUND为圆弧
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(paintSize);//设置描边宽度

        path = new Path();

        if (null == frontBitmap) {
            Drawable drawable = mContext.getResources().getDrawable(frontBitmapResource);
            int w = drawable.getIntrinsicWidth();
            int h = drawable.getIntrinsicHeight();
            Bitmap.Config config = Bitmap.Config.ARGB_8888;
            bitmap = Bitmap.createBitmap(w, h, config);
            //注意，下面三行代码要用到，否在在View或者surfaceview里的canvas.drawBitmap会看不到图
            mCanvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, w, h);
            drawable.draw(mCanvas);
        } else {
            mCanvas = new Canvas(bitmap);//设置一个带图片的画布
            mCanvas.drawBitmap(frontBitmap, 0, 0, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        float ax = event.getX();
        float ay = event.getY();

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            isMove = false;
            path.reset();
            path.moveTo(ax, ay);
            invalidate();
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            isMove = true;
            path.lineTo(ax, ay);
            invalidate();
            return true;
        }
        return super.onTouchEvent(event);
    }

    public Bitmap CreateBitmap(int color, int width, int height) {
        int[] rgb = new int[width * height];

        for (int i = 0; i < rgb.length; i++) {
            rgb[i] = color;
        }
        return Bitmap.createBitmap(rgb, width, height, Bitmap.Config.ARGB_8888);
    }
}
